package com.cyh.forum.services.impl;

import com.cyh.forum.common.AppResult;
import com.cyh.forum.common.ResultCode;
import com.cyh.forum.dao.MessageMapper;
import com.cyh.forum.exception.ApplicationException;
import com.cyh.forum.model.Message;
import com.cyh.forum.model.User;
import com.cyh.forum.services.IMessageService;
import com.cyh.forum.services.IUserService;
import com.cyh.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.boot.actuate.cache.NonUniqueCacheException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.spec.ECField;
import java.util.Date;
import java.util.List;


@Slf4j
@Service

public class MessageServiceImpl implements IMessageService {

    //  导入service层
    @Resource
    private IUserService iUserService;

    //  导入mapper层
    @Resource
    private MessageMapper messageMapper;

    @Override
    public void create(Message message) {
        //  非空校验
        if (message == null || message.getPostUserId() == null || message.getReceiveUserId() == null || StringUtil.isEmpty(message.getContent())){
            //  打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //  校验用户是否存在
        User user = iUserService.selectById(message.getReceiveUserId());
        if (user == null || user.getDeleteState() != 0){
            //  打印日志
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //  设置默认值
        message.setState((byte) 0);
        message.setDeleteState((byte) 0);
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);

        //  调用DAO层
        val row = messageMapper.insertSelective(message);
        //  校验返回结果
        if (row != 1){
            //  打印日志
            log.warn(ResultCode.FAILED_ARTICLE_REPLY_CREATE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_REPLY_CREATE));
        }
    }

    @Override
    public Integer selectUnreadCount(Long userId) {
        //  非空校验
        if (userId == null || userId <= 0){
            //  打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //  调用DAO层
        val count = messageMapper.selectUnreadCount(userId);

        //  校验查询结果
        if (count == null){
            //  打印日志
            log.warn(ResultCode.FAILED.toString() + "查询失败");
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

        //  返回结果
        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long userId) {
        //  非空校验
        if (userId == null || userId <= 0){
            //  打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //  调用DAO层
        val messages = messageMapper.selectByReceiveUserId(userId);
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        //  非空校验
        if (id == null || id <= 0 || state < 0 || state > 2){
            //  打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //  构造更新对象
        Message update = new Message();
        update.setId(id);
        update.setState(state);
        Date date = new Date();
        update.setUpdateTime(date);
        //  调用DAO层
        val row = messageMapper.updateByPrimaryKeySelective(update);
        //  校验更新结果
        if (row != 1){
            //  打印日志
            log.warn(ResultCode.FAILED_UPDATE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_UPDATE));
        }
    }

    @Override
    public Message selectById(Long id) {
        //  非空校验
        if (id == null || id <= 0){
            //  打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //  调用DAO层
        val message = messageMapper.selectByPrimaryKey(id);
        //  返回结果
        return message;
    }

    @Override
    public void reply(Long repliedId, Message message) {
        //  非空校验
        if (repliedId == null ||repliedId <= 0){
            //  打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //  将回复的私信设置为已回复
        updateStateById(repliedId, (byte) 2);
        //  将回复的内容写入数据库
        create(message);

    }



}
